﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 飞行棋案例
{
    internal class FlyingChess
    {
        public Player player = new Player();
        //存储关卡标识
        public List<int> maps = Enumerable.Repeat(0, 164).ToList();
        public List<int> luckyTurn = new List<int>();
        public List<int> landMine = new List<int>();
        public List<int> suspend = new List<int>();
        public List<int> tunnel = new List<int>();
        Random rnd = new Random();
        public FlyingChess() { }

        public void PrintMenu()
        {
            for (int i = 0; i < 7; i++)
            {
                switch (i)
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.White;
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        break;
                    case 5:
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        break;
                    case 6:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        break;
                }

                if (i == 3)
                {
                    Console.WriteLine("**************基础版飞行棋**************");
                }
                else
                {
                    Console.WriteLine("****************************************");
                }

            }
            Console.WriteLine();
        }

        public void PrintLegeng()
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.Write("\n图例\t");
            Console.Write("幸运轮盘: ◎\t");
            Console.Write("地雷: ●\t");
            Console.Write("暂停: ▲\t");
            Console.Write("时空隧道: 〓\t\n");
            Console.ForegroundColor= ConsoleColor.White;
        }


        //初始化随机道具位置
        public void InitProp()
        {
            int count = 0;
            //初始化幸运转盘位置

            count = 0;
            while (true)
            {
                int num = rnd.Next(1, 163);
                luckyTurn.Add(num);
                count++;
                if (count == 10)
                {
                    break;
                }
            }
            //初始化地雷位置
            count = 0;
            while (true)
            {
                int num = rnd.Next(1, 163);
                if (luckyTurn.Contains(num))
                {
                    continue;
                }
                landMine.Add(num);
                count++;
                if (count == 16)
                {
                    break;
                }
            }
            //初始化暂停位置
            count = 0;
            while (true)
            {
                int num = rnd.Next(1, 163);
                if (luckyTurn.Contains(num) || landMine.Contains(num))
                {
                    continue;
                }
                suspend.Add(num);
                count++;
                if (count == 8)
                {
                    break;
                }
            }
            //初始化时空隧道位置
            count = 0;
            while (true)
            {
                int num = rnd.Next(1, 163);
                if (luckyTurn.Contains(num) || landMine.Contains(num) || suspend.Contains(num))
                {
                    continue;
                }
                tunnel.Add(num);
                count++;
                if (count == 7)
                {
                    break;
                }
            }
            //幸运转盘，地雷，暂停，时空隧道位置分别对应1,2,3,4
            for (int i = 0; i < maps.Capacity - 1; i++)
            {
                foreach (var item in luckyTurn)
                {
                    if (item == i)
                    {
                        maps[i] = 1;
                    }
                }
                foreach (var item in landMine)
                {
                    if (item == i)
                    {
                        maps[i] = 2;
                    }
                }
                foreach (var item in suspend)
                {
                    if (item == i)
                    {
                        maps[i] = 3;
                    }
                }
                foreach (var item in tunnel)
                {
                    if (item == i)
                    {
                        maps[i] = 4;
                    }
                }
            }
        }
        //初始化地图
        public void DrawMap()
        {
            //第一行，输出50个□
            for (int i = 0; i < 50; i++)
            {
                DrawMapContent(i);
            }
            Console.WriteLine();
            //第一列，输出7个□
            for (int i = 50; i < 57; i++)
            {
                for (int j = 0; j < 49; j++)
                {
                    Console.Write("  ");
                }
                DrawMapContent(i);
                Console.WriteLine();
            }
            //第二行，输出50个□
            for (int i = 106; i >= 57; i--)
            {
                DrawMapContent(i);
            }
            Console.WriteLine();
            //第二列，输出7个□
            for (int i = 107; i < 114; i++)
            {
                DrawMapContent(i);
                Console.WriteLine();
            }
            //第三行，输出50个□
            for (int i = 114; i < 164; i++)
            {
                DrawMapContent(i);
            }
            Console.WriteLine();
        }
        public void DrawMapContent(int i)
        {
            //判断随机道具位置，并将随机道具放到地图对应位置
            if (i >= 0 && i < maps.Capacity)
            {
                if (player.playerPosition[0] == i && player.playerPosition[1] == i)
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    //两个玩家在同一个位置
                    Console.Write("★");
                }
                else if (player.playerPosition[0] == i)
                {
                    Console.ForegroundColor= ConsoleColor.Cyan;
                    //玩家1在这个位置
                    Console.Write("@A");
                }
                else if (player.playerPosition[1] == i)
                {
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    //玩家2在这个位置
                    Console.Write("$B");
                }
                else
                {
                    switch (maps[i])
                    {
                        case 0:
                            Console.ForegroundColor = ConsoleColor.White;
                            Console.Write("□");
                            break;
                        case 1:
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.Write("◎");
                            break;
                        case 2:
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write("●");
                            break;
                        case 3:
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.Write("▲");
                            break;
                        case 4:
                            Console.ForegroundColor = ConsoleColor.Blue;
                            Console.Write("〓");
                            break;
                        default:
                            break;
                    }
                }
                Console.ForegroundColor = ConsoleColor.White;
            }
        }

        public void StartGame(int num)
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine($"玩家{Player.names[num]} 按任意键开始掷骰子");
            Console.ReadKey();
            Console.WriteLine();

            int x = rnd.Next(1, 7);
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.WriteLine($"玩家{Player.names[num]} 掷出了{x}");
            Console.WriteLine();

            //玩家的移动位置
            player.playerPosition[num] += x;

            //玩家最多能走到163
            if (player.playerPosition[num] > 163)
            {
                player.playerPosition[num] = 163;
            }
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"玩家{Player.names[num]} 按任意键开始移动");
            Console.ReadKey();
            Console.WriteLine();

            player.ShowInformation();
            PrintLegeng();
            DrawMap();


            //玩家踩到了另一个玩家，被踩到的玩家后退6格
            if (player.playerPosition[num] == player.playerPosition[1 - num])
            {
                Console.WriteLine($"玩家{Player.names[num]}踩到了玩家{Player.names[1-num]}，" +
                    $"玩家{Player.names[1- num]}后退6格");
                player.playerPosition[1 - num] -= 6;
                if (player.playerPosition[1 - num] < 0)
                {
                    player.playerPosition[1 - num] = 0;
                }
            }
            else
            {
                switch (maps[player.playerPosition[num]])
                {
                    case 1:
                        //玩家踩到了幸运轮盘，交换玩家位置
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine($"玩家{Player.names[num]}踩到了幸运轮盘，" +
                        $"玩家{Player.names[num]}和玩家{Player.names[1 - num]}交换位置");
                        Console.ReadKey();
                        int tmp = player.playerPosition[num];
                        player.playerPosition[num] = player.playerPosition[1 - num];
                        player.playerPosition[1 - num] = tmp;
                        break;
                    case 2:
                        //玩家踩到了地雷后退6格
                        Console.ForegroundColor = ConsoleColor.DarkCyan;
                        Console.WriteLine($"玩家{Player.names[num]}踩到了地雷，" +
                        $"玩家{Player.names[num]}后退六格");
                        Console.ReadKey();
                        player.playerPosition[num] -= 6;
                        break;
                    case 3:
                        //玩家踩到了暂停，玩家暂停一回合
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine($"玩家{Player.names[num]}踩到了暂停按钮，" +
                            $"玩家暂停一回合");
                        player.playerParse[num] = true;
                        break;
                    case 4:
                        //玩家踩到了时空隧道，玩家前进10格
                        Console.ForegroundColor = ConsoleColor.Blue;
                        Console.WriteLine($"\"玩家{Player.names[num]}踩到了时空隧道按钮，"
                            + $"玩家{Player.names[num]}前进10格");
                        player.playerPosition[num] += 10;
                        break;
                }
            }
            Console.ResetColor();
            //检查玩家位置
            CheckPosition();
            player.ShowInformation();
            PrintLegeng();
            DrawMap();

            if (player.playerPosition[num] == 163)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine($"玩家{ Player.names[num]}获得胜利！");
                Win();
            }
        }

        public void CheckPosition()
        {
            if(player.playerPosition[0] < 0)
            {
                player.playerPosition[0] = 0;
            }
            if (player.playerPosition[0] > 163)
            {
                player.playerPosition[0] = 163;
            }

            if (player.playerPosition[1] < 0)
            {
                player.playerPosition[1] = 0; 
            }
            if (player.playerPosition[1] > 163)
            {
                player.playerPosition[1] = 163;
            }
        }

        public void Win()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("                                          ◆                      ");
            Console.WriteLine("                    ■                  ◆               ■        ■");
            Console.WriteLine("      ■■■■  ■  ■                ◆■         ■    ■        ■");
            Console.WriteLine("      ■    ■  ■  ■              ◆  ■         ■    ■        ■");
            Console.WriteLine("      ■    ■ ■■■■■■       ■■■■■■■   ■    ■        ■");
            Console.WriteLine("      ■■■■ ■   ■                ●■●       ■    ■        ■");
            Console.WriteLine("      ■    ■      ■               ● ■ ●      ■    ■        ■");
            Console.WriteLine("      ■    ■ ■■■■■■         ●  ■  ●     ■    ■        ■");
            Console.WriteLine("      ■■■■      ■             ●   ■   ■    ■    ■        ■");
            Console.WriteLine("      ■    ■      ■            ■    ■         ■    ■        ■");
            Console.WriteLine("      ■    ■      ■                  ■               ■        ■ ");
            Console.WriteLine("     ■     ■      ■                  ■           ●  ■          ");
            Console.WriteLine("    ■    ■■ ■■■■■■             ■              ●         ●");
            Console.ResetColor();
        }
    }
}
